<template>
	<view class="container">
		<z-paging ref="paging" v-model="dataList" @query="queryList">
			<template #top>
				<view class="container-head"></view>
				<view class="container-info">
					<view class="container-desc">
						<image src="../../static/jiqi.png" mode="aspectFill" class="container-desc-img"></image>
						<view class="container-desc-title">
							<view>总开机数</view>
							<view>{{ powerAll.total }}</view>
						</view>
					</view>

					<view class="container-card">
						<view class="container-card-item">
							<view>当日开机数</view>
							<view>{{ powerAll.todayTatol || '-' }}</view>
							<view class="line"></view>
						</view>
						<view class="container-card-item">
							<view>本周开机数</view>
							<view>{{powerAll.weekTatol || '-'}}</view>
							<view class="line"></view>
						</view>
						<view class="container-card-item">
							<view>本月开机数</view>
							<view> {{ powerAll.monthTotal || '-' }} </view>
						</view>
					</view>
				</view>

				<view class="tab-box">
					<view class="tab-box-item" :class="{'tab-box-item-active': currentIndex === 0 }"
						@click="changeIndex(0)">
						当日开机
					</view>
					<view class="tab-box-item" :class="{'tab-box-item-active': currentIndex === 1 }"
						@click="changeIndex(1)">
						{{ currentData || '选择日期'}}
						<u-datetime-picker :show="showDate" v-model="value1" mode="year-month" 
						:minDate="minDate"
						:maxDate="maxDate"
						  @confirm="confirmDate" @cancel="showDate = false">
						</u-datetime-picker>
					</view>
				</view>
			</template>

			<view class="income-centent">
				<view class="income-centent-card" v-for="(item, index) of dataList" :key="index">
					<view class="income-centent-card-title">
						<view>开机人: {{ item.head_name }}</view>
					</view>
					<view style="margin: 0 24rpx;">
						<u-line></u-line>
					</view>
					<view class="income-centent-card-main">
						<view class="income-centent-card-main-left">
							<view style="line-height: 2;">店名: {{ item.device_name }}</view>
							<view style="line-height: 1.5;">手机号码: {{ item.phone}}</view>
							<view style="line-height: 1.5;">设备ID: {{ item.device_id }}</view>
							<view style="line-height: 1.5;">开启时间: {{ item.start_time }}</view>
							<view style="line-height: 1.5;">结束时间: {{ item.end_time }}</view>
						</view>
					</view>
				</view>
			</view>
		</z-paging>
	</view>
</template>

<script>
	import request from "@/api/request.js"
	import dayJS from 'dayjs'
	export default {
		data() {
			return {
				showDate: false,
				value1: '',
				currentData: '',
				dataList: [],
				minDate: 0,
				maxDate: 0,
				machineDto: {
					token: '',
					month: dayJS().format('YYYY-MM-DD'),
					page: 1,
					page_size: 10
				},
				token: '',
				currentIndex: 0,
				powerAll: {}
			}
		},
		components: {},
		onLoad() {
			this.token = uni.getStorageSync('token')
			this.machineDto.token = this.token
			this.maxDate = Date.now()
			this.minDate = dayJS('2021-01').valueOf()
			this.getRecords()
		},
		methods: {
			confirmDate(date){
			   this.machineDto.month = dayJS(date.value).format('YYYY-MM')
			   this.currentData = this.machineDto.month
			   this.showDate = false
			    this.$refs.paging.reload()
			},
			changeIndex(index) {
				this.currentIndex = index
				if (index === 0) {
				  this.machineDto.month = dayJS().format('YYYY-MM-DD')
				 this.$refs.paging.reload()
				}
				if (index === 1) {
					this.showDate = true
				}
			},
			queryList(page, page_size) {
				uni.showLoading({
					title: '加载中...'
				})
				const that = this
				this.machineDto.page = page
				this.machineDto.page_size = page_size
				request({
					url: "/kx/Personal/powerList",
					method: "POST",
					data: this.machineDto,
					successCb(res) {
						uni.hideLoading()
						const {
							data: tempData
						} = res.data
						if (tempData) {
							res.data.data.forEach(item => {
								item.start_time = dayJS(item.start_time * 1000).format(
									'YYYY-MM-DD HH:mm:ss')
								item.end_time = dayJS(item.end_time * 1000).format('YYYY-MM-DD HH:mm:ss')
							})
						}
						that.$refs.paging.complete(res.data.data);
					},
					failCb(res) {
						uni.hideLoading()
						that.$refs.paging.complete(false);
					}
				})
			},
			getRecords() {
				uni.showLoading({
					title: '加载中...'
				})
				const that = this
				request({
					url: '/kx/Personal/getUsageRecords',
					method: "POST",
					data: {
						token: that.token
					},
					successCb(res) {
						that.powerAll = res.data || {}
						uni.hideLoading()
					},
					failCb(res) {
						uni.hideLoading()
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		background: #F4F4F4;

		&-head {
			width: 100%;
			height: 256rpx;
			background: linear-gradient(90deg, #FF8732 0%, #FFB65E 100%);
			border-radius: 0rpx 0rpx 50rpx 50rpx;
		}

		&-info {
			background: #fff;
			width: 670rpx;
			height: 294rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			margin: 0 auto;
			margin-top: -200rpx;
		}

		&-desc {
			display: flex;
			align-items: center;
			padding-top: 42rpx;
			padding-left: 52rpx;

			&-img {
				width: 118rpx;
				height: 96rpx;
				background-size: 100%;
			}

			&-title {
				margin-left: 30rpx;

				view {
					color: #3D3D3D;

					&:nth-child(1) {
						font-weight: 500;
						font-size: 24rpx;
					}

					&:nth-child(2) {
						;
						font-weight: 700;
						font-size: 60rpx;
					}
				}
			}
		}

		&-card {
			display: flex;
			justify-content: space-around;
			align-items: center;
			width: 680rpx;
			height: 134rpx;
			border-radius: 20rpx;
			margin: 0 auto;

			&-item {
				position: relative;
				display: flex;
				align-items: center;
				flex-direction: column;

				view {
					&:nth-child(1) {
						font-weight: 500;
						font-size: 24rpx;
						color: #3D3D3D;
					}

					&:nth-child(2) {
						font-weight: 500;
						font-size: 36rpx;
						color: #3D3D3D;
						line-height: 2;
					}
				}

				.line {
					position: absolute;
					right: -50rpx;
					top: 50%;
					transform: translateY(-50%);
					width: 2rpx;
					height: 44rpx;
					background-color: #D8D8D8;
				}
			}
		}
	}

	.tab-box {
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
		margin-top: 30rpx;

		&-item {
			padding: 10rpx 40rpx;
			color: #989898;

			&-active {
				color: #3D3D3D;
				font-weight: 700;
			}
		}

	}

	.income {
		&-centent {
			margin: 24rpx 0;

			&-card {
				width: 680rpx;
				background: $uni-bg-color;
				border-radius: 20rpx;
				margin: 12rpx auto;

				&-title {
					padding: 18rpx;
					font-size: 30rpx;
				}

				&-main {
					@include layout(space-between, center);
					padding: 18rpx;

					&-left {
						font-size: 24rpx;
						color: #6D6D6D;
					}

					&-right {
						font-size: 28rpx;
					}

				}
			}
		}
	}
</style>